package com.vf.cloud.paas.app.agent.common.server.signalling.timer;

import java.util.Timer;
import java.util.TimerTask;

import com.vf.cloud.paas.app.agent.common.server.signalling.pool.PlayerPool;
import com.vf.cloud.paas.app.agent.common.server.signalling.pool.StreamerPool;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UETimerTask extends TimerTask {

	private String id;
	private Timer timer;
	private int i = 0;
	private int timeout = 5;

	public UETimerTask(String id, Timer timer, int idleTimeout) {
		this.id = id;
		this.timer = timer;
		if (idleTimeout > 0) {
			this.timeout = idleTimeout;
		}
	}

	@Override
	public void run() {
		if (PlayerPool.containsToken(id)) {
			i = 0;
		} else {
			if (i > timeout) {
				if (StreamerPool.containsToken(id)) {
					StreamerPool.killByToken(id);
					timer.cancel();
					log.info(">>> Streamer>>>" + id + "<<< 超时主动关闭渲染端.");
				}
			} else {
				if (!StreamerPool.containsToken(id)) {
					timer.cancel();
					log.info(">>> Streamer>>>" + id + "<<< 渲染端已关闭.");
				} else {
					i++;
				}
			}
		}
	}
}
